/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.match.components;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.SpriteSheetCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.AvatarFrameKey;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.PawnSheetKey;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

/**
 * @author Massimo Canonaco
 *
 */
class CharacterAvatarInformationPanel016 extends JPanel 
{

	/** */
	private Map<String, ImageIcon> loadedIcons;
	
	/** */
	private SpriteSheetCache imgCache;
	
	/** */
	 private JLabel characterAvatar;

	 /** */
	 private JLabel characterName;
	 
	 /** */
	 private JLabel characterHealth;

	 /** */
	 private JLabel characterOwner;
	 
	 public CharacterAvatarInformationPanel016(SpriteSheetCache imgCache, Map<String, String> settings)
	 {
		 super(new GridBagLayout());
		 
		 OverallGUILayoutFactory gui = OverallGUILayoutFactory.getInstance();
		 Font textFont = gui.getParagraphFont().deriveFont(Float.parseFloat(settings.get("characterinformationpanel.avatarinformations.text.size")));
		 Color textColor = gui.getParsedColor(settings.get("characterinformationpanel.avatarinformations.text.color"));
//		 setOpaque(false);
//		 GridBagLayout layout = new GridBagLayout();
//		 setLayout(layout);
		 GridBagConstraints constr = new GridBagConstraints();
		 constr.gridx = GridBagConstraints.RELATIVE;
		 constr.gridy = 0;
		 
		 setBackground(gui.getParsedColor(settings.get("characterinformationpanel.avatarinformations.background.color")));
		 
		 
		 this.imgCache = imgCache;
		 loadedIcons = new HashMap<String, ImageIcon>();
		 try 
		 {
			 loadedIcons.put("noicon", new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.noicon.path")))));
//			noIcon = new ImageIcon(ImageIO.read(new FileInputStream(settings.get("characterinformationpanel.noicon.path"))));
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		 characterAvatar = new JLabel();
		 add(characterAvatar,constr);
		 
		 JPanel textInformationPanel = new JPanel();
		 BoxLayout textInfoLayout = new BoxLayout(textInformationPanel, BoxLayout.Y_AXIS);
//		 textInformationPanel.set
		 textInformationPanel.setLayout(textInfoLayout);
		 
		 characterName = new JLabel();
		 characterHealth = new JLabel();
		 characterOwner = new JLabel();
		 
		 characterName.setFont(textFont);
		 characterHealth.setFont(textFont);
		 characterOwner.setFont(textFont);
		 
		 characterName.setForeground(textColor);
		 characterHealth.setForeground(textColor);
		 characterOwner.setForeground(textColor);
		 
		 characterName.setAlignmentX(CENTER_ALIGNMENT);
		 characterHealth.setAlignmentX(CENTER_ALIGNMENT);
		 characterOwner.setAlignmentX(CENTER_ALIGNMENT);
		 
		 textInformationPanel.add(characterName);
		 textInformationPanel.add(characterHealth);
		 textInformationPanel.add(characterOwner);
		 textInformationPanel.setOpaque(false);
		 
		 add(textInformationPanel,constr);
	 }
	 
	 /** */
	 public void setLabels(Character character)
	 {
		 String characterType = character.getType();
		 if(!loadedIcons.containsKey(characterType))
		 {
			 PawnSheetKey pawnKey = new PawnSheetKey();
				pawnKey.setWho(characterType);
				pawnKey.setWhat(PawnSheetKey.FRAME_TYPE_AVATAR);
				pawnKey.setWhen("");
				
				AvatarFrameKey avatarKey = new AvatarFrameKey();
				avatarKey.setKey(AvatarFrameKey.CLASSIC_AVATAR);
				pawnKey.setFrameKey(avatarKey);
				try 
				{
					loadedIcons.put(characterType,new ImageIcon(imgCache.getImage(pawnKey, 0, true)));
				} catch (InvalidFileException e) 
				{
					// Image are inside the sheet, so this will never happen
					e.printStackTrace();
				}
		 }
		 
		 characterAvatar.setIcon(loadedIcons.get(characterType));
		 characterName.setText(character.getName());
		 characterHealth.setText(Integer.toString((int) character.getStats().getCharacteristics().getHealth()));
		 characterOwner.setText(character.getOwner().getNickname());
	 }
	/**
	 * @return the characterAvatar
	 */
	public JLabel getCharacterAvatar() {
		return characterAvatar;
	}

	/**
	 * @param characterAvatar the characterAvatar to set
	 */
	public void setCharacterAvatar(JLabel characterAvatar) {
		this.characterAvatar = characterAvatar;
	}

	/**
	 * @return the characterName
	 */
	public JLabel getCharacterName() {
		return characterName;
	}

	/**
	 * @param characterName the characterName to set
	 */
	public void setCharacterName(JLabel characterName) {
		this.characterName = characterName;
	}

	/**
	 * @return the healthBar
	 */
	public JLabel getCharacterHealth() {
		return characterHealth;
	}

	/**
	 * @param healthBar the healthBar to set
	 */
	public void setCharacterHealth(JLabel characterHealth) {
		this.characterHealth = characterHealth;
	}

	public void reset() {
		characterAvatar.setIcon(loadedIcons.get("noicon"));
		characterName.setText(" ");
		characterHealth.setText(" ");
		characterOwner.setText(" ");
	}
}
